﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls; 
using Telerik.WebControls;
using Runda.CMS.Common.Entity;
using System.Collections.Generic;
using Runda.CMS.Common.Utility;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.Workspace
{
    public partial class winSelectDeptMultiple : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateTreeView();
            }
        }
        private void GenerateTreeView()
        {
            this.RadTreeView1.Nodes.Clear();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = new SqlDbParameter();
            parm.ColumnName = "Deleted";
            parm.ColumnType = DbType.Boolean;
            parm.ParameterName = "@Deleted";
            parm.ParameterValue = false;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            List<SysDepartmentEntity> DeptList = new SysDepartmentBll().GetAll<SysDepartmentEntity>(parms, "Order By DepartmentName Asc");

            List<SysDepartmentEntity> SubDeptList = DeptList.FindAll(delegate(SysDepartmentEntity e) { return e.ParentID == Guid.Empty; });
            foreach (SysDepartmentEntity ent in SubDeptList)
            {
                RadTreeNode node = CreateNode(ent.DepartmentName, ent.DepartmentName, true, ent.DepartmentID.ToString(), true);
                RadTreeView1.Nodes.Add(node);

                RecursivelyPopulate(DeptList, node, true);
            }

        }

        private void RecursivelyPopulate(List<SysDepartmentEntity> DeptList, RadTreeNode node, bool CanEdit)
        {
            List<SysDepartmentEntity> SubDeptList = DeptList.FindAll(delegate(SysDepartmentEntity e) { return e.ParentID == new Guid(node.Value); });

            foreach (SysDepartmentEntity ent in SubDeptList)
            {
                RadTreeNode radTreeNode = CreateNode(ent.DepartmentName, ent.DepartmentName, true, ent.DepartmentID.ToString(), CanEdit);

                node.Nodes.Add(radTreeNode);
                RecursivelyPopulate(DeptList, radTreeNode, true);
            }
        }

        private RadTreeNode CreateNode(string text, string toolTip, bool expanded, string id, bool CanEdit)
        {
            RadTreeNode node = new RadTreeNode(text, id);
            node.Expanded = expanded;

            if (CanEdit)
            {
                node.ContextMenuName = "Management";
            }

            //if (id.ToLower() == Enumerator.PlanningFolderSystemId.SharedFolderId.ToLower())
            //{
            //    node.ImageUrl = "../Images/ico/ico_16_OrgChart.gif";
            //}
            //else if (id.ToLower() == Enumerator.PlanningFolderSystemId.DraftFolderId.ToLower())
            //{
            //    node.ImageUrl = "../Images/ico/ico_16_OrgChart.gif";
            //}

            return node;
        }
    }
}
